using System.Collections.Generic;
using Entitas;
using Entitas.Unity;
using UnityEngine;

public class InitViewSystem : ReactiveSystem<LogicEntity>
{
    LogicContext mContext;

    public InitViewSystem(LogicContext context) : base(context)
    {
        mContext = context;
    }

    protected override ICollector<LogicEntity> GetTrigger(IContext<LogicEntity> context)
    {
        return context.CreateCollector(LogicMatcher.ResourceInfo.Added());
    }

    protected override bool Filter(LogicEntity entity)
    {
        return true;
    }

    protected override void Execute(List<LogicEntity> entities)
    {
        foreach (var e in entities)
        {
            if (e.isCell)
            {
                GameObject go = GameObjectPool.Instance.SpawnGo(e.resourceInfo.AssetName, e.position.Value);
                go.transform.SetParent(EcsManager.CellRoot);
                CellView cellView = MatchObjectPool.Instance.SpawnCellView(go);
                go.Link(e);
                e.ReplaceLogicView(go, cellView);
            }
            else if (e.hasGrid)
            {
                GameObject go = GameObjectPool.Instance.SpawnGo(e.resourceInfo.AssetName, e.position.Value);
                go.transform.SetParent(EcsManager.GridRoot);
                go.Link(e);
                e.ReplaceLogicView(go, null);
            }
        }
    }
}